home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 676-700 / 682 / rexxhostlib / libmain.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  5KB  |  237 lines

  1. /*
  2. **    rexxhost.library - ARexx host management support library
  3. **
  4. **    Copyright © 1990-1992 by Olaf `Olsen' Barthel
  5. **        All Rights Reserved
  6. */
  7.  
  8. #include "rexxhost.h"
  9.  
  10.     /* The structure expected by the library loader (auto-init). */
  11.  
  12. struct InitTable
  13. {
  14.     ULONG     it_DataSize;    /* Data size to allocate. */
  15.     APTR    *it_FuncTable;    /* Pointer to function table. */
  16.     APTR     it_DataInit;    /* Pointer to data initializers (remember InitStruct?). */
  17.     APTR     it_InitFunc;    /* The real library init function. */
  18. };
  19.  
  20.     /* Protos for this module. */
  21.  
  22. struct RexxHostBase * __saveds __asm    LibInit(register __d0 struct RexxHostBase *RexxHostBase,register __a0 BPTR SegList);
  23. struct RexxHostBase * __saveds __asm    LibOpen(register __a6 struct RexxHostBase *RexxHostBase);
  24. BPTR __saveds __asm            LibClose(register __a6 struct RexxHostBase *RexxHostBase);
  25. BPTR __saveds __asm            LibExpunge(register __a6 struct RexxHostBase *RexxHostBase);
  26.  
  27.     /* Pointer to library segment list. */
  28.  
  29. BPTR             LibSegList;
  30.  
  31.     /* ASCII library ID. */
  32.  
  33. UBYTE __aligned LibName[]    = "rexxhost.library";
  34. UBYTE __aligned LibID[]        = VSTRING;
  35.  
  36.     /* File version string. */
  37.  
  38. STATIC UBYTE VersionTag[]    = VERSTAG;
  39.  
  40.     /* Global library base IDs. */
  41.  
  42. struct RxsLib        *RexxSysBase;
  43. struct ExecBase        *SysBase;
  44.  
  45.     /* The list of library functions. */
  46.  
  47. APTR __aligned LibFuncTab[] =
  48. {
  49.     LibOpen,        /* Standard library routines. */
  50.     LibClose,
  51.     LibExpunge,
  52.     NULL,
  53.  
  54.     CreateRexxHost,        /* Now for the real stuff. */
  55.     DeleteRexxHost,
  56.     SendRexxCommand,
  57.     FreeRexxCommand,
  58.     ReplyRexxCommand,
  59.     GetRexxCommand,
  60.     GetRexxArg,
  61.     GetRexxResult1,
  62.     GetRexxResult2,
  63.     GetToken,
  64.     GetStringValue,
  65.     BuildValueString,
  66.     RexxStrCmp,
  67.     GetRexxMsg,
  68.     SendRexxMsg,
  69.     GetRexxString,
  70.     GetRexxClip,
  71.  
  72.     (APTR)-1        /* End marker. */
  73. };
  74.  
  75.     /* The romtag needs this. */
  76.  
  77. struct InitTable LibInitTab =
  78. {
  79.     sizeof(struct RexxHostBase),    /* Lib base. */
  80.     LibFuncTab,            /* Function table. */
  81.     NULL,                /* No data init table (we'll do autoinit). */
  82.     LibInit                /* Lib init routine. */
  83. };
  84.  
  85.     /* LibInit(RexxHostBase,SegList):
  86.      *
  87.      *    Does the main library initialization, expects
  88.      *    all arguments in registers.
  89.      */
  90.  
  91. struct RexxHostBase * __saveds __asm
  92. LibInit(register __d0 struct RexxHostBase *RexxHostBase,register __a0 BPTR SegList)
  93. {
  94.     SysBase = *(struct ExecBase **)4;
  95.  
  96.         /* Remember segment list. */
  97.  
  98.     LibSegList = SegList;
  99.  
  100.         /* Fill in the library node head. */
  101.  
  102.     RexxHostBase -> LibNode . lib_Node . ln_Type    = NT_LIBRARY;
  103.     RexxHostBase -> LibNode . lib_Node . ln_Name    = LibName;
  104.  
  105.         /* Set the remaining flags. */
  106.  
  107.     RexxHostBase -> LibNode . lib_Flags        = LIBF_SUMUSED | LIBF_CHANGED;
  108.     RexxHostBase -> LibNode . lib_Version        = VERSION;
  109.     RexxHostBase -> LibNode . lib_Revision        = REVISION;
  110.     RexxHostBase -> LibNode . lib_IdString        = (APTR)LibID;
  111.  
  112.         /* Return the result (surprise!). */
  113.  
  114.     return(RexxHostBase);
  115. }
  116.  
  117.     /* LibOpen():
  118.      *
  119.      *    Library open routine.
  120.      */
  121.  
  122. struct RexxHostBase * __saveds __asm
  123. LibOpen(register __a6 struct RexxHostBase *RexxHostBase)
  124. {
  125.     if(!RexxHostBase -> LibNode . lib_OpenCnt)
  126.     {
  127.         if(!(RexxHostBase -> RexxSysBase = RexxSysBase = (struct RxsLib *)OpenLibrary(RXSNAME,0)))
  128.             return(NULL);
  129.     }
  130.  
  131.         /* Increment open count and prevent delayed
  132.          * expunges.
  133.          */
  134.  
  135.     RexxHostBase -> LibNode . lib_OpenCnt++;
  136.     RexxHostBase -> LibNode . lib_Flags &= ~LIBF_DELEXP;
  137.  
  138.         /* Return base pointer. */
  139.  
  140.     return(RexxHostBase);
  141. }
  142.  
  143.     /* LibClose():
  144.      *
  145.      *    Closes the library.
  146.      */
  147.  
  148. BPTR __saveds __asm
  149. LibClose(register __a6 struct RexxHostBase *RexxHostBase)
  150. {
  151.     BPTR SegList = ZERO;
  152.  
  153.         /* Is the library user count ok? */
  154.  
  155.     if(RexxHostBase -> LibNode . lib_OpenCnt)
  156.     {
  157.             /* Decrement user count. */
  158.  
  159.         RexxHostBase -> LibNode . lib_OpenCnt--;
  160.  
  161.             /* Try the expunge. */
  162.  
  163.         SegList = LibExpunge(RexxHostBase);
  164.     }
  165.     else
  166.     {
  167.         /* One close request after the lib has already
  168.          * shut down? We'll call Mr. Guru.
  169.          */
  170.  
  171.         Alert(AT_Recovery | AG_CloseLib,RexxHostBase);
  172.     }
  173.  
  174.         /* Return the segment list, ramlib will know
  175.          * what to do with it.
  176.          */
  177.  
  178.     return(SegList);
  179. }
  180.  
  181.     /* LibExpunge(RexxHostBase):
  182.      *
  183.      *    Expunge library, careful: this can be called by
  184.      *    ramlib without the rest of the library knowing
  185.      *    about it.
  186.      */
  187.  
  188. BPTR __saveds __asm
  189. LibExpunge(register __a6 struct RexxHostBase *RexxHostBase)
  190. {
  191.     BPTR SegList = ZERO;
  192.  
  193.         /* Is the user count zero, the delayed expunge flag
  194.          * set and do we have a valid segment list?
  195.          */
  196.  
  197.     if(!RexxHostBase -> LibNode . lib_OpenCnt && (RexxHostBase -> LibNode . lib_Flags & LIBF_DELEXP) && LibSegList)
  198.     {
  199.             /* Remember segment list. */
  200.  
  201.         SegList = LibSegList;
  202.  
  203.             /* Set real segment list to zero which will
  204.              * hopefully keep us from getting expunged
  205.              * twice.
  206.              */
  207.  
  208.         LibSegList = ZERO;
  209.  
  210.             /* Remove library from lib list. */
  211.  
  212.         Remove(&RexxHostBase -> LibNode . lib_Node);
  213.  
  214.             /* Close the libraries. */
  215.  
  216.         if(RexxSysBase)
  217.             CloseLibrary(RexxSysBase);
  218.  
  219.             /* Free library/jump table memory. */
  220.  
  221.         FreeMem((BYTE *)((ULONG)RexxHostBase - RexxHostBase -> LibNode . lib_NegSize),RexxHostBase -> LibNode . lib_NegSize + RexxHostBase -> LibNode . lib_PosSize);
  222.     }
  223.     else
  224.     {
  225.         /* In any other case we'll set the delayed
  226.          * expunge flag (so next expunge call will
  227.          * hopefully wipe us from the lib list).
  228.          */
  229.  
  230.         RexxHostBase -> LibNode . lib_Flags |= LIBF_DELEXP;
  231.     }
  232.  
  233.         /* Return segment list pointer. */
  234.  
  235.     return(SegList);
  236. }
  237.